package com.dang.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dang.dao.ReceiveAddressDAO;
import com.dang.model.ReceiveAddress;
import com.dang.util.DBConnection;
import com.mysql.jdbc.Statement;

public class ReceiveAddressDAOImp implements ReceiveAddressDAO {
	private static final String FIND_BY_ID="select * from d_receive_address where id=?";
	private static final String SAVE="insert into d_receive_address(id,user_id,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?,?)";
	private static final String FIND_BY_USER_ID="select * from d_receive_address where user_id=?";

	@Override
	public ReceiveAddress findById(int id) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement prep= null;
		Connection conn= null;
		ReceiveAddress addr=null;
		ResultSet rst=null;
		
		try{
			conn= DBConnection.getConnection();
			prep= conn.prepareStatement(FIND_BY_ID,Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1, id);
			rst=prep.executeQuery();
			if(rst.next()){
				addr=new ReceiveAddress();
				addr.setId(rst.getInt("id"));
				addr.setUserId(rst.getInt("user_id"));
				addr.setReceiveName(rst.getString("receive_name"));
				addr.setFullAddress(rst.getString("full_address"));
				addr.setPostalCode(rst.getString("postal_code"));
				addr.setMobile(rst.getString("mobile"));
				addr.setPhone(rst.getString("phone"));
			}

		} finally {
			DBConnection.close(rst,prep, conn);
		}
		return addr;
	}

	@Override
	public List<ReceiveAddress> findByUserId(int userId) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement prep= null;
		Connection conn= null;
		ResultSet rst=null;
		List<ReceiveAddress> addrs=new ArrayList<ReceiveAddress>();
		
		try{
			conn= DBConnection.getConnection();
			prep= conn.prepareStatement(FIND_BY_USER_ID);
			prep.setInt(1, userId);
			rst=prep.executeQuery();
			while(rst.next()){
				ReceiveAddress addrss=new ReceiveAddress();
				addrss.setId(rst.getInt("id"));
				addrss.setUserId(rst.getInt("user_id"));
				addrss.setReceiveName(rst.getString("receive_name"));
				addrss.setFullAddress(rst.getString("full_address"));
				addrss.setPostalCode(rst.getString("postal_code"));
				addrss.setMobile(rst.getString("mobile"));
				addrss.setPhone(rst.getString("phone"));
				addrs.add(addrss);
			}

		} finally {
			DBConnection.close(rst,prep, conn);
		}
		return addrs;
	}

	@Override
	public void save(ReceiveAddress addr) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement prep= null;
		Connection conn= null;
		ResultSet rst=null;
		
		try{
			conn= DBConnection.getConnection();
			prep= conn.prepareStatement(SAVE,Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1,addr.getId());
			prep.setInt(2, addr.getUserId());
			prep.setString(3, addr.getReceiveName());
			prep.setString(4, addr.getFullAddress());
			prep.setString(5, addr.getPostalCode());
			prep.setString(6, addr.getMobile());
			prep.setString(7, addr.getPhone());
			prep.executeUpdate();
		

		} finally {
			DBConnection.close(rst,prep, conn);
		}
	}

}
